#include "summarydialog.h"
#include "ui_summarydialog.h"
#include <QFile>
#include <QTextStream>
#include <QFileInfo>
#include <QFileDialog>
SummaryDialog::SummaryDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SummaryDialog)
{
    ui->setupUi(this);

    setWindowFlags(Qt::WindowCloseButtonHint| Qt::Dialog);

    QStringList header;
    header<<tr("Date")<<tr("Lot")<<"Total"<<tr("Pass")<<"Fail"<<"FR"<<"Version"<<"Ball Damage" << "Cross Mark" << "Substrate" << "Capacitor";
    ui->tableWidget->setHorizontalHeaderLabels(header);
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
    QHeaderView * h = ui->tableWidget->horizontalHeader();
    h->setSectionResizeMode(QHeaderView::Stretch);

	connect(ui->btnExport, &QPushButton::clicked, [=]() {
		//QString str = QString("%1").arg(QCoreApplication::applicationDirPath());
        QString path = QFileDialog::getSaveFileName(this, tr("Result document"), QCoreApplication::applicationDirPath(), "*.csv", Q_NULLPTR);

		if (!path.isEmpty())
		{
			QFile file(path);
			file.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
			QTextStream text_stream(&file);
			QMap<QString, ResultManager::DATE_RESULT_DEF>::const_iterator it = m_res.constBegin();
            text_stream << "Date,"<<"Lot," << "Total," << "Pass," << "Fail," << "FR," << "Version," <<"Ball Damage," << "Cross Mark," << "Substrate," << "Capacitor"<< endl;
			while (it != m_res.constEnd()) {
				QString str = it.key();
				ResultManager::DATE_RESULT_DEF d = it.value();
                text_stream << str << ","<<"N/A" <<","<< d.total << "," << d.ok << "," << d.ng << "," << QString("%1%").arg(QString::number(d.ng*100.0 / d.total, 'f', 2)) 
					<< "," <<d.version<<","<<d.mark.iBallDamage<<"," << d.mark.iCrossMark << "," << d.mark.iSubstrateDamage << "," << d.mark.iCap  << endl;
				it++;
			}
		}
	});

	connect(ui->btnQuit, &QPushButton::clicked, [=]() {
		QDialog::reject();
	});
}

SummaryDialog::~SummaryDialog()
{
    delete ui;
}

void SummaryDialog::SetReslut(QMap<QString, ResultManager::DATE_RESULT_DEF> res)
{
	m_res = res;
	QMap<QString, ResultManager::DATE_RESULT_DEF>::const_iterator it = res.constBegin();
	
    while (it != res.constEnd()) {
        int count = ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(count);
        QString str = it.key();
        ResultManager::DATE_RESULT_DEF d = it.value();
        {

            QTableWidgetItem *item = new QTableWidgetItem(str);
            item->setTextAlignment(Qt::AlignCenter);
//            item->setTextColor(QColor(200,111,100));
            item->setFont(QFont("Helvetica", 12));

            ui->tableWidget->setItem(count, 0, item);
        }
		{
			QTableWidgetItem *item = new QTableWidgetItem("N/A");
			item->setTextAlignment(Qt::AlignCenter);
			//            item->setTextColor(QColor(200,111,100));
			item->setFont(QFont("Helvetica", 12));

			ui->tableWidget->setItem(count, 1, item);
		}
        {
            QTableWidgetItem *item = new QTableWidgetItem(QString::number(d.total));
            item->setTextAlignment(Qt::AlignCenter);
//            item->setTextColor(QColor(200,111,100));
            item->setFont(QFont("Helvetica", 12));

            ui->tableWidget->setItem(count, 2, item);
        }
        {
            QTableWidgetItem *item = new QTableWidgetItem(QString::number(d.ok));
            item->setTextAlignment(Qt::AlignCenter);
            item->setTextColor(QColor(0,200,0));
            item->setFont(QFont("Helvetica", 12));

            ui->tableWidget->setItem(count, 3, item);
        }
        {
            QTableWidgetItem *item = new QTableWidgetItem(QString::number(d.ng));
            item->setTextAlignment(Qt::AlignCenter);
            item->setTextColor(QColor(200,0,0));
            item->setFont(QFont("Helvetica", 12));

            ui->tableWidget->setItem(count, 4, item);
        }
		{
			
			QTableWidgetItem *item = new QTableWidgetItem(QString("%1%").arg(QString::number(d.ng*100.0 / d.total, 'f', 2)));
			item->setTextAlignment(Qt::AlignCenter);
			//            item->setTextColor(QColor(200,111,100));
			item->setFont(QFont("Helvetica", 12));

			ui->tableWidget->setItem(count, 5, item);
        }
        {

            QTableWidgetItem *item = new QTableWidgetItem(d.version);
            item->setTextAlignment(Qt::AlignCenter);
            //            item->setTextColor(QColor(200,111,100));
            item->setFont(QFont("Helvetica", 12));

            ui->tableWidget->setItem(count, 6, item);
        }
		{

			QTableWidgetItem *item = new QTableWidgetItem(QString::number(d.mark.iBallDamage));
			item->setTextAlignment(Qt::AlignCenter);
			//            item->setTextColor(QColor(200,111,100));
			item->setFont(QFont("Helvetica", 12));

			ui->tableWidget->setItem(count, 7, item);
		}
		{

			QTableWidgetItem *item = new QTableWidgetItem(QString::number(d.mark.iCrossMark));
			item->setTextAlignment(Qt::AlignCenter);
			//            item->setTextColor(QColor(200,111,100));
			item->setFont(QFont("Helvetica", 12));

			ui->tableWidget->setItem(count, 8, item);
		}
		{

			QTableWidgetItem *item = new QTableWidgetItem(QString::number(d.mark.iSubstrateDamage));
			item->setTextAlignment(Qt::AlignCenter);
			//            item->setTextColor(QColor(200,111,100));
			item->setFont(QFont("Helvetica", 12));

			ui->tableWidget->setItem(count, 9, item);
		}
		{

			QTableWidgetItem *item = new QTableWidgetItem(QString::number(d.mark.iCap));
			item->setTextAlignment(Qt::AlignCenter);
			//            item->setTextColor(QColor(200,111,100));
			item->setFont(QFont("Helvetica", 12));

			ui->tableWidget->setItem(count, 10, item);
		}

        it++;
    }
}
